package com.hnkywl.sts.service.sys;

import com.hnkywl.sts.dao.sys.QxDao;
import com.hnkywl.sts.entity.sys.Qx;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 说明：
 * 包:        com.hnkywl.sts.service.sys
 * 类描述:    权限服务类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Service
public class QxService {
    private QxDao qxDao;
    private Long qxId;

    /**
     * 查询权限列表
     *
     * @param qxmc     权限名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findAllQx(String qxmc, int pageNo, int pageSize) {
        Page<Qx> qxPage = this.qxDao.findQxList(qxmc, pageNo, pageSize);
        List<Qx> qxList = qxPage.getResult() == null ? new ArrayList<Qx>() : qxPage.getResult();
        Map<String,Object> otherParam = new HashMap<String, Object>();
        otherParam.put("list_ymysList","mc");
        otherParam.put("list_gnczList","mc");
        String qxJson = BaseUtils.fetchCollectionToJson(qxList,otherParam, "id", "mc", "cd.mc", "gnczList", "ymysList","cd.id");
        return "{\"total\":" + qxPage.getTotalCount() + ",\"rows\":[" + qxJson + "]}";
    }

    /**
     * 查询权限
     *
     * @param id 权限Id
     * @return
     */
    @Transactional
    public Qx findQxDetail(Long id) {
        return this.qxDao.get(id);
    }

    /**
     * 保存权限信息
     *
     * @param qx 权限
     * @return
     */
    @Transactional
    public void saveQx(Qx qx) {
        nullId(qx);
        if (qxDao.checkUnique(qx.getId(),qx.getMc(),"mc") ) {
            throw new RuntimeException("权限名称不能重复！");
        }
        qx.setDateCreated(new Date());
        qx.setLastUpdated(qx.getDateCreated());
        this.qxDao.save(qx);
    }

    /**
     * 删除权限信息
     *
     * @param qxId 权限Id
     * @return
     */
    @Transactional
    public void deleteQx(Long qxId) {
        Qx qx = qxDao.get(qxId);
        if (!qx.getJsList().isEmpty()) {
            throw new RuntimeException("该权限已被使用，不能删除！");
        }
        qx.setLastUpdated(new Date());
        qx.setDeleted(1);
        this.qxDao.save(qx);
    }

    @Transactional
    public String findAllQxmc(String qxmc, int pageNo, int pageSize) {
        Page<Qx> qxPage = this.qxDao.findQxList(qxmc, pageNo, pageSize);
        List<Qx> qxList = qxPage.getResult() == null ? new ArrayList<Qx>() : qxPage.getResult();
        String qxJson = BaseUtils.fetchCollectionToJson(qxList, "id", "mc");
        return "[" + qxJson + "]";
    }

    /**
     * 传入一个权限id中间用，构成的字符串 返回所属机构的集合
     *
     * @return
     */
    public List<Qx> findQxbyString(String qxIdList) {
        if ("".equals(qxIdList)) {
            return null;
        }
        List<Qx> qxs = new ArrayList<Qx>();
        String[] s1 = qxIdList.split(",");
        for (String id : s1) {
            Qx q = new Qx();
            q.setId(Long.parseLong(id));
            qxs.add(q);
        }
        return qxs;
    }
    /**
     * ajax远程验证
     * @param value 验证值
     * @param field 验证字段
     * @return
     */
    public boolean ajaxValidate(Long id,String value, String field) {
            return qxDao.checkUnique(id,value,field) ;
    }

    /**
     *   对空值进行判断
     * @param qx
     */
    private void nullId(Qx qx){
        if (qx != null && qx.getGnczList() != null && qx.getGnczList().size() != 0 && qx.getGnczList().get(0).getId() == null)
            qx.setGnczList(null);
        if (qx != null && qx.getCd() != null && qx.getCd().getId() == null)
            qx.setCd(null);
        if (qx != null && qx.getYmysList() != null && qx.getYmysList().size() !=0 &&  qx.getYmysList().get(0).getId() == null)
            qx.setYmysList(null);
    }
    public Long getQxId() {
        return qxId;
    }

    public void setQxId(Long qxId) {
        this.qxId = qxId;
    }

    @Autowired
    public void setQxDao(QxDao qxDao) {
        this.qxDao = qxDao;
    }
}
